package CircuitLogic;
import static java.lang.Math.*;
/*
	电路元件中的直流稳压电源
 */
public class VoltageStabilizedPowerSource extends Components
{
	final double eps = 1e-10;
	final double maxElectricity = 20;
	private double voltage;
	private boolean switchButton;
	private double resistanceLoad;

	VoltageStabilizedPowerSource()
	{
		voltage = 0;
		switchButton = false;
		resistanceLoad = 0;
	}

	/*
		设定电压时，要求电压值范围为0-10V
		设定成功当且仅当返回true
		设定失败将保持设定值不变
	 */
	public boolean setVoltage(double voltage)
	{
		if(voltage>10)
		{
			return false;
		}
		else
		{
			this.voltage = voltage;
			return true;
		}
	}

	public double getVoltage()
	{
		return voltage;
	}

	public double getOutputVoltage()
	{
		if(switchButton)
		{
			return voltage;
		}
		else
		{
			return 0;
		}
	}

	/*
		请在按下开关前设定加载电阻
		若载入电阻为0，即短路，则返回false，且开关弹起
	 */
	public boolean pressSwitchButton()
	{
		switchButton = true;
		if(abs(resistanceLoad)<eps||voltage/resistanceLoad>maxElectricity)
		{
			releaseSwitchButton();
			return false;
		}
		else
		{
			return true;
		}
	}

	public void releaseSwitchButton()
	{
		switchButton = false;
	}

	public double getElectricity()
	{
		if(switchButton)
		{
			return voltage/resistanceLoad;
		}
		else
		{
			return 0;
		}
	}

	public void setResistanceLoad(double RLoad)
	{
		if(RLoad<eps && switchButton)
		{
			releaseSwitchButton();
		}
		else
		{
			this.resistanceLoad = RLoad;
		}
	}

	public boolean getSwitchButton()
	{
		return switchButton;
	}

}
